package com.sinodata.bsm.center.dao.impl;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.sinodata.bsm.center.dao.BaseDao;
import com.sinodata.bsm.center.dao.NotifyRuleDao;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-6 PM 3:57:37      liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Repository
public class NotifyRuleDaoImpl extends BaseDao implements NotifyRuleDao {

    @Override
    public void delete(Long id) {
        String hql = "delete from NotifyRule where id=" + id.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @Override
    public void deleteNotifyReceiveRule(Long id) {
        String hql = "delete from NotifyReceiveRule where notifyRuleId=" + id.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @Override
    public void deleteNotifyReceiveRuleByNotifyReceiveRuleId(Long id) {
        String hql = "delete from NotifyReceiveRule where id=" + id.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @Override
    public void deleteNotifyPauseRule(Long id) {
        String hql = "delete from NotifyPauseRule where notifyRuleId=" + id.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @Override
    public void deleteNotifyPauseRuleByNotifyPauseRuleId(Long id) {
        String hql = "delete from NotifyPauseRule where id=" + id.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @Override
    public void deleteNotifyExcludeResRule(Long id) {
        String hql = "delete from NotifyExcludeResRule where notifyRuleId=" + id.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @Override
    public void deleteNotifyExcludeResRuleByNotifyExcludeResRuleId(Long id) {
        String hql = "delete from NotifyExcludeResRule where id=" + id.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @Override
    public void deleteNotifyEventTypeRule(Long id) {
        String hql = "delete from NotifyEventTypeRule where notifyRuleId=" + id.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @Override
    public void deleteNotifyDelayRule(Long id) {
        String hql = "delete from NotifyDelayRule where notifyRuleId=" + id.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @Override
    public void deleteNotifyDelayRuleByNotifyDelayRuleId(Long id) {
        String hql = "delete from NotifyDelayRule where id=" + id.longValue();
        getSession().createQuery(hql).executeUpdate();
    }

    @Override
    public void batchDeleteNotifyEventTypeRule(Long[] ids) {
        this.getSession().createQuery("delete from NotifyEventTypeRule where id in (:ids)").setParameterList("ids", ids).executeUpdate();
    }

    @Override
    public int getByNameAndResId(Long resId, String name) {
        StringBuilder hql = new StringBuilder(" select count(*) from NotifyRule where res_id = " + resId + " and name ='" + name + "'");
        Query query = this.getSession().createQuery(hql.toString());
        return ((Long) query.list().iterator().next()).intValue();
    }
}